﻿<?php

class HomeController extends Controller
{
	public function filters()
	{
		return array(
			'rights', // perform access control for CRUD operations

		);
	}
	public function allowedActions()
	{
		return '*';
	}
	public function actionIndex()
	{
		$model = array();
		$model['socap'] = ActicleCategories::model()->findByPk(10);
		$model['trungcap'] = ActicleCategories::model()->findByPk(11);
		$model['caocap'] = ActicleCategories::model()->findByPk(12);
		$slides = HomeSlides::model()->findAll();
		$this->pageTitle = $this->title;
		$this->render('index',compact('model','slides'));
	}
	public function actionAbout(){
		$model = Infomations::model()->findByPk(1);
		$this->pageTitle = "Gới thiệu - ".$this->title;
		$this->render('about',compact('model'));
	}
	public function actionNews(){
		$this->pageTitle = "Bảng chử cái - ".$this->title;
		$this->render('news');
	}
	public function actionTutorial(){
		$this->pageTitle = "Hướng dẫn - ".$this->title;
		$this->render('tutorial');
	}
	public function actionQuestion(){
		$this->pageTitle = "Hỏi đáp - ".$this->title;
		$this->render('question');
	}
	public function actionLibrary(){
		$this->layout = 'main_two';
		$this->pageTitle = "Thư viện - ".$this->title;
		
		$criteria = new CDbCriteria();
		$criteria->order = "t.id DESC";
		$criteria->condition = 't.acticle_category_id = 3';
		
		$total = Acticles::model()->count($criteria);
		$pages = new CPagination($total);
		$pages->pageSize = 10;
		$pages->applyLimit($criteria);
		$model = Acticles::model()->findAll($criteria);
		
		$this->render('library',compact('model','pages'));
	}
	public function actionContact(){
		$modelSys = SystemInfo::model()->findByPk(1);
		
		if(!empty($_POST['Contacts'])){
			$model = new Contacts;
			$model->attributes = $_POST['Contacts'];
			$model->created = time();
			if($model->validate()){
				$mail = new YiiMailer();
				//$mail->clearLayout();//if layout is already set in config
				$mail->setFrom($_POST['Contacts']['email'], $_POST['Contacts']['name']);
				$mail->setTo($modelSys->email);
				$mail->setSubject('[CheapShop] - Email liên hệ từ - '.$_POST['Contacts']['name']);
				$mail->setBody($_POST['Contacts']['content']);
				$mail->send();
				jsonOut(array('erorr'=>false));

			}else{
				jsonOut(array(
					'error'=>true,
					'data'=>$model->errors
				));
			}
			Yii::app()->end();
		}
		
		$this->pageTitle = "Liên hệ";
		$this->render('contact',array('model'=>$modelSys));
	}
	public function actionError()
	{
		$this->pageTitle = "Error";
		if($error=Yii::app()->errorHandler->error)
		{
			if(Yii::app()->request->isAjaxRequest)
				echo $error['message'];
			else
				$this->render('error', $error);
		}
	}
}